package com.situ.onlinebookstore.front.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.onlinebookstore.common.entity.Cart;
import com.situ.onlinebookstore.common.entity.Goods;
import com.situ.onlinebookstore.common.mapper.CartMapper;
import com.situ.onlinebookstore.common.mapper.GoodsMapper;
import com.situ.onlinebookstore.front.service.CartService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CartServiceImpl implements CartService {
    private final CartMapper cartMapper;
    private final GoodsMapper goodsMapper;

    @Override
    @Transactional //支持事务
    public int add(Cart cart) throws Exception {
        //先检查数据库是不是已经有了
        Cart sCart = cartMapper.selectByUserIdAndGoodsId(cart.getUserId(), cart.getGoodsId());
        //校验
        if (ObjectUtils.isEmpty(sCart)) {
            Goods goods = goodsMapper.selectById(cart.getGoodsId());
            if (ObjectUtils.isEmpty(goods)) {
                throw new Exception("商品不存在！");
            }
            if (goods.getStatus().equals(1)) {
                throw new Exception("商品已下架！");
            }
            if (goods.getCount() < cart.getCount()) {
                throw new Exception("库存不足！");
            }
            //保存到数据库中
            return cartMapper.insert(cart);
        } else {
            sCart.setCount(sCart.getCount() + 1);
            //更新数据库
            return this.edit(sCart);
        }
    }

    @Override
    @Transactional
    public int remove(Cart cart) throws Exception {
        Cart sCart = cartMapper.selectByIdAndUserId(cart.getId(), cart.getUserId());
        if (ObjectUtils.isEmpty(sCart)) {
            throw new Exception("无效购物车！");
        }
        return cartMapper.delete(cart.getId());
    }

    @Override
    @Transactional
    public int edit(Cart cart) throws Exception {
        Cart sCart = cartMapper.selectByIdAndUserId(cart.getId(), cart.getUserId());
        if (ObjectUtils.isEmpty(sCart)) {
            throw new Exception("无效购物车！");
        }
        Goods goods = goodsMapper.selectById(sCart.getGoodsId());
        if (goods.getStatus().equals(1)) {
            //删除当前购物车信息
            remove(cart);
            throw new Exception("商品已下架！");
        }
        if (goods.getCount() < cart.getCount()) {
            cart.setCount(goods.getCount());
            //更新数据库
            cartMapper.update(cart);
            throw new Exception("库存不足！");
        }
        //更新数据库
        return cartMapper.update(cart);
    }

    @Override
    public PageInfo getByUserId(Integer page, Integer limit, Integer userId) {
        PageHelper.startPage(page,limit);
        List list = cartMapper.selectByUserId(userId);
        return new PageInfo(list);
    }

    @Override
    public Cart getById(Integer id) {
        return cartMapper.selectById(id);
    }

    @Override
    public List getByIds(Integer[] cartIds) {
        return cartMapper.selectByIds(cartIds);
    }
}
